<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Merek extends CI_Controller {
	private $limit=10;

	function __construct(){
		parent::__construct();
		
		$index	= $this->config->item('index_page');
		$host	= $this->config->item('base_url');

		$this->url = empty($index) ? $host : $host . $index . '/';
		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
		$this->load->model('mMerek');
		
		//get session.. 'user' tuh nama session yang udah di set 
//		$this->user	= unserialize(base64_decode($this->session->userdata('user')));		
	}
	
	public function index($offset=0,$order_column='id_merek', $order_type='asc'){
	if (!empty($this->user)){
		if(empty($offset)) $offset=0;
		if(empty($order_column)) $order_column='id_merek';
		if(empty($order_type)) $order_type='asc';
		//todo : check for valid_barang column
		
		//load data siswa
		$mereks=$this->mMerek->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
		//generate pagination
		$config['base_url']=site_url('admin/produk/index/');
		$config['total_rows']=$this->mMerek->count_all();
		$config['per_page']=$this->limit;
		$config['uri_segment']=3;
		$this->pagination->initialize($config);
		$data['pagination']=$this->pagination->create_links();
		
		//generate table data
		$this->table->set_empty("&nbsp;");
		$new_order=($order_type=='asc'?'desc':'asc');
		$this->table->set_heading(
			'ID MEREK','NAMA MEREK','ACTIONS');
		foreach($mereks as $merek){
				$this->table->add_row($merek->ID_MEREK,$merek->NAMA_MRK,anchor('admin/merek/update/'.$merek->ID_MEREK,'update',array('class'=>'update')).' '.anchor('admin/merek/delete/'.$merek->ID_MEREK,'delete',array('class'=>'delete','onclick'=>"return confirm('Apakah Anda yakin ingin menghapus data merek?')"))
				);
		}
			
			$data['table']=$this->table->generate();
			
			if($this->uri->segment(3)=='delete success') $data['message']='Data berhasil dihapus';
			else if ($this->uri->segment(3)=='add_success') $data['message']='Data berhasil ditambah';
			else $data['message']='';
			//load view
	
		$data['url'] = $this->url;
		$data['header'] = $this->load->view('admin/header/header', $data, true);
		$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
		$data['content'] = $this->load->view('admin/rcontent/merek/merek', $data, true);
		$this->load->view('index', $data);
	}
	else
	{
		 //If no session, redirect to login page
		 redirect('admin/login');
	}
	}
	
	function add() {
	//set common properties
	$data['title']='tambah merek baru';
	$data['action']=site_url('admin/merek/add');
	$data['link_back']=anchor('admin/merek/index/','Back to list of merek',array('class'=>'back'));
	
	$this->_set_rules();
	
	//run valid_barangation
	if($this->form_validation->run()===FALSE){
		$data['message']='';
		//set common properties
		$data['title']='tambah merek baru';
		$data['message']='';
		$data['merek']['ID_MEREK']='';
		$data['merek']['NAMA_MRK']='';
		$data['link_back']=anchor('admin/merek/index','Lihat daftar merek',array('class'=>'back'));
		$data['url'] = $this->url;
		$data['header'] = $this->load->view('admin/header/header', $data, true);
		$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
		$data['content'] = $this->load->view('admin/rcontent/merek/merekEdit', $data, true);

		$this->load->view('index',$data);
	} else{
		//save data
		$merek=array('id_merek'=>$this->input->post('id_merek'),'nama_mrk'=>$this->input->post('nama_mrk'));
		$id_merek=$this->mMerek->save($merek);
		
		//set form input nama_brg="id_barang"
		$this->validation->id_merek=$id_merek;
		
		redirect('admin/merek/index/add_success');
	}
	}
	function update($id_merek){
	//set common properties
	$data['title']='Update merek';
	
	//set validation properties
		$this->_set_rules();
		$data['action']=('admin/merek/update/'.$id_merek);
		
		//run validation
		if($this->form_validation->run()===FALSE){
			$data['message']='';
			$data['merek']=$this->mMerek->get_by_id($id_merek)->row_array();
			
			//set common properties
			$data['title']='Update Merek';
			$data['message']='';
		}else {
			//save data
			$id_merek=$this->input->post('id_merek');
			$merek=array('id_merek'=>$this->input->post('id_merek'),'nama_mrk'=>$this->input->post('nama_mrk'));
			$this->mMerek->update($id_merek,$merek);
			$data['merek']=$this->mMerek->get_by_id($id_merek)->row_array();
			
			//set user message
			$data['message']='update merek berhasil';
		}
		$data['link_back']=anchor('admin/merek/index/','Lihat daftar merek',array('class'=>'back'));
		//load view
		$data['url'] = $this->url;
		$data['header'] = $this->load->view('admin/header/header', $data, true);
		$data['menu'] = $this->load->view('admin/menu/menu', $data, true);
		$data['content'] = $this->load->view('admin/rcontent/merek/merekEdit', $data, true);

		$this->load->view('index',$data);
	}
	
	function delete($id_merek){
		//delete siswa
		$this->mMerek->delete($id_merek);
		//redirect to siswa list page
		redirect('admin/merek/index/delete_success','refresh');
	}
	
	//valid_barangation rules
	function _set_rules(){
		$this->form_validation->set_rules('nama_mrk','nama_mrk','required|trim');
	}


	
}